<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title>设置</title>
		<link rel="stylesheet" href="../css/mui.css">
		<link rel="stylesheet" href="../css/bootstrap.min.css">
		<script src="../js/mui.js"></script>
		<script src="../js/jquery.min.js"></script>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">设置</h1>
		</header>

		<div class="mui-content">
			<div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<span style="font-size: 1.5em;" id="courseName">课程名</span>
						<span class="mui-icon mui-icon-spinner mui-spin" style="color:orange; font-size: 1.2em;"></span>
						<br><br><br>
					</div>
				</div>
			</div>

			<form class="mui-input-group">
				<div class="mui-input-row">
					<label>课程ID</label>
					<input type="text" placeholder="课程ID" id="course_id" disabled="disabled">
				</div>
				<div class="mui-input-row">
					<label>课程名称</label>
					<input type="text" placeholder="课程名称" id="course_name">
				</div>
				<div class="mui-input-row">
					<label>上课班级</label>
					<input type="text" placeholder="授课班级, 多个班级请用逗号分割" id="course_class">
				</div>
				<div class="mui-button-row">
					<button type="button" class="mui-btn mui-btn-primary" id="as">确认</button>&nbsp;&nbsp;
					<button type="button" class="mui-btn mui-btn-danger">取消</button>
				</div>
			</form>
		</div>

		<script>
			//mui初始化
			mui.init({
				swipeBack: true, //启用右滑关闭功能
			});
			var cID;
			var uID;
			mui.plusReady(function() {
				cID = plus.webview.currentWebview().cID;
				uID = plus.storage.getItem("uID");
				init();
			})


			function init() {
				mui.ajax({
					url: 'http://120.27.247.180:8080/School/course/queryCourseByCID',
					type: "post",
					data: {
						"cID": cID
					},
					dataType: "jsonp",
					contentType: "application/x-www-form-urlencoded; charset=utf-8",
					jsonp: "jsoncallback",
					success: function(data) {
						data = JSON.parse(data);
						var courseId = jQuery("#course_id");
						courseId.val(cID);
						var courseName = jQuery("#courseName");
						courseName.text(data.course['cName']);
						var course_name = jQuery("#course_name");
						course_name.val(data.course['cName']);
						var courseClass = jQuery("#course_class");
						courseClass.val(data.course['className']);
					},
					error: function() {
						mui.toast("获取课程信息失败");
					}
				});
			}

			//修改课程信息
			document.getElementById("as").addEventListener('tap', function(e) {
				e.detail.gesture.preventDefault();
				var course_name = document.getElementById('course_name').value;
				var course_class = document.getElementById('course_class').value;
				// console.log(course_class);
				// console.log(course_name);
				mui.ajax({
					url: 'http://120.27.247.180:8080/School/course/updateCourse',
					type: "post",
					data: {
						"cID": cID,
						"uID": uID,
						"cName": course_name,
						"className": course_class,
					},
					dataType: "jsonp",
					contentType: "application/x-www-form-urlencoded; charset=utf-8",
					jsonp: "jsoncallback",
					success: function(data) {
						//console.log(data);
						data = JSON.parse(data);
						if (data['suc']) {
							mui.alert('修改成功', '提示', function() {
								init();
							});
						} else {
							mui.alert('修改失败', '提示', function() {});
						}
					},
					error: function() {
						mui.toast("服务器异常，请稍后再试");
					}
				});
			});
		</script>
	</body>
</html>
